<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./static/static/vue.min.js"></script>
    <script src="./static/static/axios.min.js"></script>


    <!-- 本地引入lib-master -->
    <link rel="stylesheet" type="text/css" href="./static/lib-master/theme-chalk/index.css" />

    <script src="./static/lib-master/index.js" type="text/javascript" charset="utf-8"></script>
</head>

<body>
    <div id='app'>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="手机号" prop="id">
                <el-input v-model="ruleForm.id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
                <el-button @click="add()">注册</el-button>
            </el-form-item>
        </el-form>

        <el-dialog title="注册" :visible.sync="dialogFormVisible">
            <el-form :model="vo">
                <el-form-item label="手机号" :label-width="formLabelWidth">
                    <el-input v-model="vo.id" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="用户名" :label-width="formLabelWidth">
                    <el-input v-model="vo.nickname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input v-model="vo.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="上传头像" :label-width="formLabelWidth">
                    <el-upload action="http://localhost:88/api/user/user/upload" class="upload-demo"
                        :on-success="handlePreview" :on-remove="handleRemove" :file-list="fileList" list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过10MB</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="register()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</body>
<script>
    new Vue({
        el: '#app',
        data() {
            var validateid = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入手机号'));
                } else {
                    if (this.ruleForm.password !== '') {
                        this.$refs.ruleForm.validateField('password');
                    }
                    callback();
                }
            };
            var validateid2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            return {
                baseUrl: "http://localhost:88/api",
                formLabelWidth: '120px',
                dialogTableVisible: false,
                dialogFormVisible: false,
                ruleForm: {
                    id: '',
                    password: '',
                },
                rules: {
                    id: [
                        { validator: validateid, trigger: 'blur' }
                    ],
                    password: [
                        { validator: validateid2, trigger: 'blur' }
                    ]
                },
                vo: { head: "" },
                fileList:[]
            };
        },
        //方法集合
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post(this.baseUrl + '/user/user/login', this.ruleForm).then(({ data }) => {
                            if(data.code==0) {
                                console.log(data)
                                alert("登录成功跳转到秒杀活动列表");
                                localStorage.setItem("token",data.user.token)
                                localStorage.setItem("id",data.user.id)
                                location.href='activeList.html';
                            } else {
                                alert("用户名或密码错误")
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            add() {
                this.dialogFormVisible = true;
                this.vo = {}
                this.fileList=[]
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePreview(file) {
                console.log(file);
                this.vo.head = file.url
            },
            register() {
                axios.get(this.baseUrl + '/user/user/phone?id=' + this.vo.id).then(({ data }) => {
                    if (data.code == 0) {
                        axios.post(this.baseUrl + '/user/user/loginAdd', this.vo).then(({ data }) => {
                            if (data.code == 0) {
                                alert("注册成功")
                                this.dialogFormVisible = false
                            }
                        })
                    } else {
                        alert("手机号重复,请重新输入")
                    }
                })
            },

        }
    })
</script>

</html>